// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.*;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.model.nav.after.sell.order.NavAfterSellOrderVo;
import com.fowo.api.model.nav.logistics.provider.NavLogisticsProviderSearchParamPo;
import com.fowo.api.model.nav.sell.order.*;
import com.fowo.api.model.product.CustomDeclareToProductActionVo;
import com.fowo.api.service.impl.NavSellOrderServiceImpl;
import com.fowo.api.service.impl.ProductServiceImpl;
import com.fowo.api.sys.annotation.RedisSyncLock;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "订单管理")
@RequestMapping("/api/nav-sell-order")
@RestController
public class NavSellOrderCustomController {

    @Resource
    private NavSellOrderServiceImpl service;
    @Resource
    private ProductServiceImpl productService;




    /*自定义开始
     * */


    @Operation(summary = "设定仓库物流")
    @PostMapping("logisticsWarehouseSet")
    @RedisSyncLock(value = "NavSellOrder_logisticsWarehouseSet", timeout = 30)
    public R logisticsWarehouseSet(@RequestBody CustomNavSellOrderLogisticsWarehouseSetActionVo po) {
        return R.run(() -> service.customLogisticsWarehouseSet(po));
    }


    @Operation(summary = "编辑订单信息")
    @PostMapping("editOrder")
    @RedisSyncLock(value = "NavSellOrder_editOrder", timeout = 30)
    public R editOrder(@RequestBody CustomNavSellOrderEditOrderActionVo po) {
        return R.run(() -> service.customEditOrder(po.getPo()));
    }

    @Operation(summary = "获取可合并订单分页查询结果")
    @GetMapping("joinPageSearch/{withChild}")
    public R<Page<?>> getJoinPageSearch(@PathVariable("withChild") boolean withChild, NavSellOrderSearchParamPo search) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(service.joinPageSearch(search, withChild));
    }


//    @Operation(summary = "获取配对分页查询结果")
//    @GetMapping("selectProduct")
//    public R<Page<?>> selectProduct(NavSellOrderSelectProductParam search) {
//        return R.data(service.selectProduct(search));
//    }


    @Operation(summary = "拆分")
    @PostMapping("split")
    @RedisSyncLock(value = "NavSellOrder_split", timeout = 30)
    public R split(@RequestBody CustomNavSellOrderSprlitActionVo po) {
        return R.run(() -> service.split(po.getId(), po.getSplitList()));
    }

    @Operation(summary = "标记标签")
    @PostMapping("customchangeTag")
    @RedisSyncLock(value = "NavSellOrder_changeTag", timeout = 30)
    public R customChangeTag(@RequestBody CustomNavSellOrdeChangeTagActionVo po) {
        return R.run(() -> service.changeTag(po.getIdList(), po.getTag(), po.getRemoveTag()));
    }

    @Operation(summary = "客服备注")
    @PostMapping("recordRemark")
    @RedisSyncLock(value = "NavSellOrder_recordRemark", timeout = 30)
    public R recordRemark(@RequestBody CustomNavSellOrderRecordRemarkActionVo po) {
        return R.run(() -> service.customRecordRemark(po));
    }


    @Operation(summary = "创建并提交售后订单")
    @PostMapping("customCreateAndSubmitAfterSellOrder")
    @RedisSyncLock(value = "NavSellOrder_customCreateAndSubmitAfterSellOrder", timeout = 30)
    public R<Long> customCreateAndSubmitAfterSellOrder(@RequestBody NavAfterSellOrderVo model) {
        return R.data(() -> service.customCreateAndSubmitAfterSellOrder(model));
    }


    @Operation(summary = "打单拣货树形列表")
    @GetMapping("order-picking")
    @RedisSyncLock(value = "NavSellOrder_orderPicking", timeout = 30)
    public R<List<CustomNavLogisticsProviderVo>> orderPicking(
            NavLogisticsProviderSearchParamPo search
    ) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(() -> service.searchTree(search));
    }

    @Operation(summary = "获取运单状态和轨迹接口:FL231109001")
    @GetMapping("get_bill_track/{orderId}")
    public R<Object> GetBillTrack(@PathVariable String orderId) {
        return R.data(() -> service.getBillTrack(orderId));
    }


    @Operation(summary = "获取打单,面单,发货单打印主表信息分页查询结果")
    @GetMapping("selectCustomPrint")
    @RedisSyncLock(value = "NavSellOrder_selectCustomPrint", timeout = 30)
    public R<List<?>> selectCustomPrint(NavSellOrderSelectPrintParam search) throws Exception {
        List<?> customNavSellOrderSelectPrintResults = service.selectCustomPrint(search);
        return R.data(customNavSellOrderSelectPrintResults);
    }

    @Operation(summary = "获取标签数量")
    @GetMapping("selectTagCount")
    @RedisSyncLock(value = "NavSellOrder_selectTagCount", timeout = 30)
    public R<List<NavSellOrderSelectTagCountResult>> selectTagCount(NavSellOrderSearchParamPo search) {
        return R.data(service.selectCustomTagCount(search));
    }

    @Operation(summary = "快捷编辑")
    @PostMapping("customQuickEdit")
    @RedisSyncLock(value = "NavSellOrder_customQuickEdit", timeout = 30)
    public R customQuickEdit(@RequestBody CustomNavSellOrderQuickEditActionVo model) {
        return R.run(() -> service.customQuickEdit(model));
    }

    @Operation(summary = "更新报关信息")
    @PostMapping("customDeclareToProduct")
    @RedisSyncLock(value = "NavSellOrder_customDeclareToProduct", timeout = 30)
    public R customDeclareToProduct(@RequestBody CustomNavSellOrderDeclareToProductActionVo vo) {
        return R.run(() -> service.customDeclareToProduct(vo));
    }

    @Operation(summary = "报关信息所有子表查询")
    @PostMapping("getVoByIdList")
    public R<List<NavSellOrderVo>> getVoByIdList(@RequestBody IdListPo idList) {
        return R.data(() -> service.getDeclareVoListByIdList(idList.getIds()));
    }

    @Operation(summary = "批量编辑报关信息")
    @PostMapping("customBatchDeclareToProduct")
    @RedisSyncLock(value = "NavSellOrder_customBatchDeclareToProduct", timeout = 30)
    public R customBatchDeclareToProduct(@RequestBody CustomNavSellOrderDeclareToProductListActionVo voList) {
        return R.run(() -> service.customDeclareToProductList(voList.getList()));
    }



    @Operation(summary = "批量修改产品")
    @PostMapping("batchUpdateProduct")
    public R customBatchUpdateProduct(@RequestBody CustomDeclareToProductActionVo vo) {
        return R.run(() -> productService.customBatchUpdateProduct(vo));
    }
}
